<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <!-- <crudOperation :permission="permission" /> -->
      <!-- 表单渲染 -->
      <el-dialog title="编辑物料" :visible.sync="editDialog" width="70%" @close="handlerCancel">
        <el-table ref="materialTable" border :data="materialList" size="small" style="width: 100%;">
          <el-table-column prop="receiverName" label="客户" />
          <el-table-column prop="productNo" label="料号" />
          <el-table-column prop="productDescription" label="物料描述" />
          <el-table-column prop="partnerProductNo" label="客户物料号" />
          <el-table-column prop="qty" label="当前数量" />
          <el-table-column label="修改后数量">
            <template slot-scope="scope">
              <div v-if="scope.row.reviewStatus === 0">
                {{ scope.row.modifyQty }}
              </div>
              <el-input v-else v-model.number="scope.row.modifyQty" :placeholder="scope.row.modifyQty" />
            </template>
          </el-table-column>
          <el-table-column prop="netWeight" label="重量" />
          <el-table-column prop="inboundTime" label="入库时间" />
          <el-table-column prop="reviewName" label="审核人" />
          <el-table-column prop="reviewStatus" label="审核状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.reviewStatus === 0" type="primary">未审核</el-tag>
              <el-tag v-else-if="scope.row.reviewStatus === 1" type="success">通过</el-tag>
              <el-tag v-else type="danger">未通过</el-tag>
            </template>
          </el-table-column>
        </el-table>
        <div v-if="materialList.length > 0 && materialList.every(item => item.reviewStatus === 0)" slot="footer" class="dialog-footer">
          <el-button :loading="btn.judge === 2" :disabled="btn.judge === 1" type="primary" @click="judgeValidate(2)">审核未通过</el-button>
          <el-button :loading="btn.judge === 1" :disabled="btn.judge === 2" type="success" @click="judgeValidate(1)">审核通过</el-button>
        </div>
        <div v-else slot="footer" class="dialog-footer">
          <el-button type="text" @click="handlerCancel">取消</el-button>
          <el-button :loading="btn.confirmUpdate" type="primary" @click="confirmSubmit">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="inventoryCheckCode" label="盘点单号" />
        <el-table-column prop="productNo" label="料号" />
        <el-table-column prop="productDescription" label="物料描述" />
        <el-table-column prop="partnerProductNo" label="客户物料编号" />
        <el-table-column prop="receiverName" label="客户" />
        <el-table-column prop="parentPalletCode" label="母托编码" />
        <el-table-column prop="storageCode" label="储位编码" />
        <el-table-column prop="status" label="状态" />
        <el-table-column prop="boxesQty" label="箱数" />
        <el-table-column prop="inventoryQty" label="库存数量" />
        <el-table-column prop="checkingQty" label="盘点数量" />
        <el-table-column prop="checkTime" label="盘点时间" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="scope">
            <div style="display: flex;gap: 5px;">
              <el-button type="primary" size="mini" icon="el-icon-edit" @click="handlerEdit(scope.row)">
                修改物料
              </el-button>
              <!-- <el-button type="danger" size="mini" @click="judgeOpen(scope.row.id)">审核</el-button> -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudInventoryCheckRecord from '@/api/work/inventoryCheckRecord'
import { getMaterialInfoAPI, confirmUpdateAPI, judgeAPI } from '@/api/work/inventoryCheckRecord'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, inventoryCheckCode: null, productNo: null, productDescription: null, partnerProductNo: null, receiverName: null, parentPalletCode: null, storageCode: null, status: null, boxesQty: null, inventoryQty: null, checkingQty: null, checkTime: null, updateTime: null, createTime: null, createName: null, updateName: null }
export default {
  name: 'MaterialDetail',
  components: { pagination },
  // dicts: ['storage_type'],
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'inventoryCheckRecord', url: 'api/inventoryCheckRecord', idField: 'id', sort: 'id,desc', crudMethod: { ...crudInventoryCheckRecord },
      queryOnPresenterCreated: false
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'inventoryCheckRecord:add', 'otherUser'],
        edit: ['admin', 'inventoryCheckRecord:edit', 'otherUser'],
        del: ['admin', 'inventoryCheckRecord:del', 'otherUser']
      },
      rules: {
      },
      selectVisible: false,
      btn: {
        judge: '',
        confirmUpdate: false
      },
      editForm: {

      },
      editDialog: false,
      materialList: [],
      selectPalletId: []
    }
  },
  created() {
    this.query.inventoryCheckCode = this.$route.query.inventoryCheckCode
    this.query.productNo = this.$route.query.productNo
  },
  mounted() {
    this.crud.toQuery()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    judgeOpen(id) {
      this.selectId = id
      this.selectVisible = true
    },
    selectClose() {
      this.selectVisible = false
      this.selectId = ''
    },
    async judgeValidate(val) {
      try {
        if (val === 1) {
          this.btn.judge = 1
        } else {
          this.btn.judge = 2
        }
        const submitForm = {
          recordIds: this.selectPalletId.filter(item => item),
          isReview: val
        }
        console.log(submitForm)
        const res = await judgeAPI(submitForm)
        if (res.result === 'OK') {
          this.$message.success('操作成功')
          this.crud.toQuery()
        } else {
          return this.$message.warning(res.data)
        }
      } finally {
        this.btn.judge = ''
        this.editDialog = false
      }
    },
    // 根据母托获取所有物料信息
    async handlerEdit(row) {
      this.selectPalletId.push(row.id)
      // console.log(this.selectPalletId)
      const res = await getMaterialInfoAPI(row.parentPalletCode)
      // console.log(res)
      if (res.result === 'OK') {
        if (res.data) {
          this.materialList = res.data.materials.map(item => {
            if (item.reviewStatus === 0 || !item.reviewStatus) {
              return {
                ...item,
                materialId: item.id,
                currentQty: item.qty,
                modifyQty: item.modifyQty
              }
            } else {
              return {
                ...item
              }
            }
          })
          console.log(this.materialList)
          this.editDialog = true
        } else {
          return this.$message.warning('该母托无物料信息')
        }
      } else {
        return this.$message.warning(res.data)
      }
    },
    handlerCancel() {
      this.editDialog = false
      this.materialList = []
      this.selectPalletId = []
    },
    async confirmSubmit() {
      // console.log(this.materialList)
      const flag = this.materialList.every(item => item.modifyQty !== 0 && item.modifyQty !== '')
      // console.log(flag)
      if (!flag) return this.$message.warning('修改的数量不能为0或空')
      const submitForm = {
        inventoryCheckReviewList: this.materialList
      }
      console.log(submitForm)
      this.confirmUpdate = true
      try {
        const res = await confirmUpdateAPI(submitForm)
        if (res.result === 'OK') {
          this.$message.success(res.message)
          this.crud.toQuery()
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.materialList = []
        this.confirmUpdate = true
        this.editDialog = false
        this.selectPalletId = []
      }
    }
  }
}
</script>

<style scoped>

</style>
